from msilib.schema import Icon
from os import set_inheritable
import PySimpleGUI as sg
import yaml
import json
def json_to_yaml(data):
    return yaml.dump(data[0],allow_unicode=True,sort_keys=False)

def yaml_to_json(data):
    return yaml.load(stream=data,Loader=yaml.FullLoader)

red_x_base64 = sg.red_x
sg.theme('DarkBlack')


layout = [
          [sg.VPush()],
          [sg.Push(),sg.Text('yaml <==> json 相互转换',size=(20,1),font=('Arial',20)),sg.Push()],
          [sg.Text('   yaml'),sg.Push(),sg.Text('            json'),sg.Push()],
          [sg.Push(),sg.Multiline(key='-IN1-',size=(50,50),font=("Arial",11)),sg.Button('<=',key='-JandY-'),sg.Button('=>',key='-YandJ-'),sg.Multiline(key='-IN2-',size=(50,50),font=("Arial",11)),sg.Push()],
          [sg.Push(),sg.Text('对yaml与json之间互相转换'),sg.Push()],
          [sg.VPush()]
          ]


window = sg.Window('格式转换', layout, alpha_channel=.95, grab_anywhere=True,size=(980,800),icon=r'.\ico.ico')

while True:  # Event Loop
    event, values = window.read()
    #print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == '-JandY-':
        yaml_data = json_to_yaml([json.loads(str(values['-IN2-']).replace('\'','"'))])
        window['-IN1-'].update(yaml_data)
    if event == '-YandJ-':
        yaml_data = yaml_to_json(values['-IN1-'])
        window['-IN2-'].update(json.dumps(yaml_data,sort_keys=False,indent=4,separators=(', ',': '),ensure_ascii=False))

window.close()